<template>
  <div class="cloud_shop_rank">
    <c-title :hide="false" :text="'排行榜'"></c-title>
    <div class="cloud_shop_rankBox">
      <div class="tabBox">
        <van-tabs v-model="active" @change="tabChang" line-width="36px" background="transparent" color="#fff" title-active-color="#ffffff" title-inactive-color="#fff">
          <van-tab :title="item.name" :title-style="item.id == active ? 'font-weight: bold;font-size: 1rem;' : ''" v-for="(item, index) in first_category_list" :key="index" :name="item.id"></van-tab>
        </van-tabs>
      </div>
      <div class="rankTop" v-if="dataList.length > 0">
        <div class="rankTop_lis" v-if="dataList_f(1)" @click="toDetail(dataList_f(1))">
          <div class="pImg">
            <div class="txt">{{ dataList_f(1).rank }}</div>
            <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/cloud_shop%2Ftop2.png" alt="" />
          </div>
          <img :src="dataList_f(1).thumb" alt="" class="rankTop_lis_image" />
          <div class="bandImg" v-if="dataList_f(1).brand_logo">
            <img :src="dataList_f(1).brand_logo" alt="" />
          </div>
          <div class="price">{{ $i18n.t("money") }}{{ dataList_f(1).price }}</div>
          <div class="original_price">{{ $i18n.t("money") }}{{ dataList_f(1).market_price }}</div>
        </div>
        <div class="emptyBox" v-if="!dataList_f(1)"></div>

        <div class="rankTop_lis one" :key="index" v-if="dataList_f(0)" @click="toDetail(dataList_f(0))">
          <div class="pImg">
            <div class="txt">{{ dataList_f(0).rank }}</div>
            <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/cloud_shop%2Ftop1.png" alt="" />
          </div>
          <img :src="dataList_f(0).thumb" alt="" class="rankTop_lis_image image_one" />
          <div class="bandImg" v-if="dataList_f(0).brand_logo">
            <img :src="dataList_f(0).brand_logo" alt="" />
          </div>
          <div class="price">{{ $i18n.t("money") }}{{ dataList_f(0).price }}</div>
          <div class="original_price">{{ $i18n.t("money") }}{{ dataList_f(0).market_price }}</div>
        </div>

        <div class="rankTop_lis" :key="index" v-if="dataList_f(2)" @click="toDetail(dataList_f(2))">
          <div class="pImg">
            <div class="txt">{{ dataList_f(2).rank }}</div>
            <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/cloud_shop%2Ftop3.png" alt="" />
          </div>
          <img :src="dataList_f(2).thumb" alt="" class="rankTop_lis_image" />
          <div class="bandImg" v-if="dataList_f(2).brand_logo">
            <img :src="dataList_f(2).brand_logo" alt="" />
          </div>
          <div class="price">{{ $i18n.t("money") }}{{ dataList_f(2).price }}</div>
          <div class="original_price">{{ $i18n.t("money") }}{{ dataList_f(2).market_price }}</div>
        </div>
        <div class="emptyBox" v-if="!dataList_f(2)"></div>
      </div>
      <div class="rankBottom">
        <template v-for="(item, index) in dataList">
          <div class="rank_lis" v-if="item.rank > 3" :key="index">
            <img :src="item.thumb" alt="" class="rank_lis_image" />
            <div class="rank_list_r">
              <div class="title_box ">
                <div class="tag" v-if="item.brand_logo"><img :src="item.brand_logo" alt="" /></div>
                {{ item.title }}
              </div>
              <div class="price_btn">
                <div class="price_btn_l">
                  <div class="price">{{ $i18n.t("money") }}{{ item.price }}</div>
                  <div class="original_price">{{ $i18n.t("money") }}{{ item.market_price }}</div>
                </div>
                <div class="price_btn_r" @click="toDetail(item)">立即购买</div>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active: "0",
      first_category_list: [],
      page: 1,
      total_page: 0,
      isLoadMore: true,
      dataList: []
    };
  },
  activated() {
    this.init();
    this.getData();
  },
  methods: {
    toDetail(item) {
      this.$router.push(this.fun.getUrl("goods", { id: item.id }));
    },
    tabChang() {
      this.init();
      this.getList();
    },
    init() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.dataList = [];
    },
    async getList() {
      let { data, msg, result } = await $http.post("plugin.cloud-shop.frontend.day-sales-rank.goodsList", { first_category_id: this.active }, "");
      if (result) {
        this.dataList = data.list.data;
        this.total_page = data.list.last_page;
        this.isLoadMore = true;
      } else {
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let json = {
          page: this.page,
          first_category_id: this.active
        };
        let { data, msg, result } = await $http.post("plugin.cloud-shop.frontend.day-sales-rank.goodsList", json, "");
        if (result) {
          this.isLoadMore = true;
          this.dataList = this.dataList.concat(data.list.data);
        } else {
          this.page = this.page - 1;
          this.$toast(msg);
        }
      }
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.cloud-shop.frontend.day-sales-rank.index", {}, "");
      if (result) {
        this.first_category_list = data.first_category_list;
        if (this.first_category_list.length > 0) {
          this.active = this.first_category_list[0].id;
          this.init();
          this.getList();
        }
      } else {
        this.$toast(msg);
      }
    }
  },
  computed: {
    dataList_f(key) {
      return function(key) {
        return this.dataList[key];
      };
    }
  }
};
</script>
<style lang="scss" scoped>
.cloud_shop_rank {
  .cloud_shop_rankBox {
    background-image: url("https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/cloud_shop%2FrankBg.png");
    background-position: 0 0;
    background-size: 100% 20.34rem;
    background-repeat: no-repeat;
    min-height: 100vh;
    padding-top: 6.09rem;
    box-sizing: border-box;
    .tabBox {
      box-sizing: border-box;
    }
  }
  .rankBottom {
    margin: 0.63rem 0.75rem;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    .rank_lis {
      background: #ffffff;
      padding: 0.63rem;
      border-radius: 0.63rem;
      display: flex;
      justify-content: space-between;
      margin-bottom: 0.66rem;
      .rank_lis_image {
        margin: 0;
        width: 6.25rem;
        height: 6.25rem;
        border-radius: 0.5rem;
        margin-right: 0.69rem;
        flex-shrink: 0;
      }
      .price_btn {
        display: flex;
        justify-content: space-between;
        margin-top: 1.03rem;
        .price_btn_r {
          padding: 0.59rem 0.72rem;
          box-sizing: border-box;
          font-weight: 500;
          font-size: 0.81rem;
          color: #ffffff;
          background: #f15353;
          border-radius: 1.88rem;
        }
        .price_btn_l {
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          .price {
            font-weight: 500;
            font-size: 0.94rem;
            color: #f15353;
          }
          .original_price {
            font-size: 0.75rem;
            color: #aaaab3;
            text-decoration-line: line-through;
          }
        }
      }
      .rank_list_r {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .title_box {
          display: flex;
          align-items: flex-start;
          text-align: left;
          font-weight: 500;
          font-size: 0.88rem;
          color: #00001c;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          text-overflow: ellipsis;
          .tag {
            display: inline-flex;
            flex-shrink: 0;
            background: #ffffff;
            border-radius: 0.31rem 0.31rem 0.31rem 0.31rem;
            border: 0.03rem solid #d6d6dc;
            box-sizing: border-box;
            margin-right: 0.3rem;
            img {
              width: 1.81rem;
              height: 1.15rem;
              border-radius: 0.31rem 0.31rem 0.31rem 0.31rem;
            }
          }
        }
      }
    }
  }
  .rankTop {
    margin: 0.63rem 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    .emptyBox {
      width: 6.5rem;
      height: 9.72rem;
      opacity: 0;
    }
    .emptyBox_first {
      width: 7.63rem;
      height: 11rem;
      opacity: 0;
    }
    .rankTop_lis {
      padding: 0.31rem 0.28rem;
      padding-bottom: 0.75rem;
      box-sizing: border-box;
      background: linear-gradient(177deg, #ffede8 0%, #ffffff 100%);
      border-radius: 0.63rem;
      margin-top: 0.81rem;
      position: relative;
      .rankTop_lis_image {
        width: 5.94rem;
        height: 5.94rem;
        border-radius: 0.5rem;
        margin: 0;
      }
      .bandImg {
        width: 3.06rem;
        height: 1.91rem;
        background: #ffffff;
        border-radius: 0.44rem;
        margin: 0 auto;
        margin-top: -1.1rem;
        position: relative;
        border: 0.03rem solid #d6d6dc;
        margin-bottom: 0.38rem;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .price {
        font-weight: 500;
        font-size: 0.88rem;
        color: #f15353;
        margin-top: 0.38rem;
        margin-bottom: 0.25rem;
      }
      .original_price {
        font-size: 0.75rem;
        color: #999999;
        text-decoration-line: line-through;
      }
      .image_one {
        width: 6.94rem;
        height: 6.94rem;
      }
    }
    .pImg {
      position: absolute;
      left: 0.31rem;
      top: 0;
      .txt {
        position: absolute;
        font-weight: bold;
        font-size: 0.88rem;
        color: #935211;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        text-align: center;
        margin-top: 0.7rem;
      }
      img {
        width: 2rem;
        height: 2rem;
      }
    }
    .one {
      margin-top: 0;
    }
  }
}
</style>
